from django.template import loader, RequestContext
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect, Http404
from garetjax.gallery.models import Photo, Set, import_folder, Collection
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from garetjax.gallery.forms import AlbumImportForm, SetEditForm, PhotoFormset
from garetjax import config

def import_album(request):
    if request.method == 'POST':
        import_form = AlbumImportForm(request.POST)
        
        if import_form.is_valid():
            new_set = import_form.save()
            folder = import_form.cleaned_data['album_folder']
            
            import_folder(folder, new_set)
            
            return HttpResponseRedirect(reverse('photos.set.edit', kwargs={'set_id': new_set.id}))
    else:
        import_form = AlbumImportForm()
    
    return render_to_response('gallery/backend/import.html', {
        'form': import_form,
    }, RequestContext(request))

def xhr_get_photos(request, container, container_id):
    if container == 'set':
        objects = Photo.objects.filter(set=container_id).all()
    elif container == 'collection':
        objects = Photo.objects.filter(set__collection=container_id).all()
    else:
        raise Http404('Invalid query')
    
    return render_to_response('gallery/backend/blocks/key_photo_selector.html', {
        'o': objects
    })

def display_collection(request, collection_id):
    return render_to_response('gallery/display_collection.html', {
        'collection': get_object_or_404(Collection, pk=collection_id),
    }, RequestContext(request))

def display_set(request, set_id):
    set = get_object_or_404(Set, pk=set_id)
    photos = Paginator(set.photos.all(), int(config.get('photos.photos_per_page')))
    
    try:
        page_num = int(request.GET.get('page', '1'))
    except ValueError:
        page_num = 1
    
    try:
        page = photos.page(page_num)
    except (EmptyPage, InvalidPage):
        page = photos.page(photos.num_pages)
        
    
    return render_to_response('gallery/display_set.html', {
        'set': set,
        'photos': page,
    }, RequestContext(request))
    
def display_photo(request, photo_id):
    return render_to_response('gallery/display_photo.html', {
        'photo': get_object_or_404(Photo, pk=photo_id),
    }, RequestContext(request))

def index(request):
    return render_to_response('gallery/index.html', {
        'collections': Collection.objects.all(),
    }, RequestContext(request))
    

def edit_set(request, set_id):
    set = get_object_or_404(Set, pk=set_id)

    if request.method == 'POST':
        photo_formset = PhotoFormset(request.POST, queryset=set.photos.all())
        set_form = SetEditForm(request.POST, instance=set)
        
        if set_form.is_valid() and photo_formset.is_valid():
            set_form.save()
            photo_formset.save()
    else:
        set_form = SetEditForm(instance=set)
        photo_formset = PhotoFormset(queryset=set.photos.all())
    
    return render_to_response('gallery/backend/edit_set.html', {
        'set': set,
        'form': set_form,
        'forms': photo_formset,
    }, RequestContext(request))
    